<template>

  <el-dialog
    title="分配权限"
    :visible="showDialog"
    @close="closeFn"
  >
    <el-tree
      ref="tree"
      node-key="id"
      :data="permissionList"
      :props="defaultProps"
      show-checkbox
      check-strictly
      default-expand-all
    />

    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="comfirmFn">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>

</template>

<script>
import { getperm } from '@/api/permissions.js'
import { listTotree } from '@/utils'
import { editRole, getRoleDetail } from '@/api/roles.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      permissionList: [], // 权限列表
      permIds: [], // 选中的权限列表
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      roleId: ''

    }
  },
  created() {
    this.getperm()
  },
  methods: {
    async getperm() {
      const res = await getperm()

      this.permissionList = listTotree(res.data, '0')
    },
    async getRoleList(id) {
      this.roleId = id
      const res = await getRoleDetail(id)
      this.permIds = res.data.permissions
      console.log(64, this.permIds)
      this.$refs.tree.setCheckedNodes(this.permIds)
    },
    async comfirmFn() {
      this.permIds = this.$refs.tree.getCheckedKeys()
      console.log(66, this.permIds)
      // 调用接口，更新权限
      await editRole({
        id: this.roleId,
        permissions: this.permIds
      })
      this.closeFn()
    },
    closeFn() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
